import Router from "@koa/router";
import svgCaptcha from "svg-captcha";
import moment from "moment";
import { getValue, setValue } from "../../config/RedisConfig"

const router = new Router();

router.prefix("/public");
router.get("/captcha", (ctx) => {
  const body = ctx.request.query;
  const newCaptcha = svgCaptcha.create({
    size: 4,
    ignoreChars: "0o1il",
    color: true,
    noise: Math.floor(Math.random() * 4),
    width: 150,
    height: 50,
  });

  // console.log(newCaptcha);
  // {
  //   text: "",
  //   data: 验证码
  // }

  // 设置验证码超时时间, 单位: s 
  setValue(body.sid, newCaptcha.text, 10 * 60);
  
  ctx.body = {
    code: 200,
    data: newCaptcha.data,
  };
});

router.get("/code", (ctx) => {
  ctx.body = {
    code: 200,
    data: {
      time: moment().add(30, "minutes").format("YYYY-MM-DD HH:mm:ss"),
    },
  };
});

export default router;
